##Acknowledgements:
##  Infinite Recursion - IR-GTS
##  LordLandon - sendpkm.py
##  Project Pokemon - http://projectpokemon.org/wiki/GTS_protocol
##  YoshiOG1 - inspiration to make this

from Tkinter import *
from tables.prettytable import PrettyTable
import thread


class rootWindow(Tk):
	def _sendpkm_thread(self,foo,barr):
		self._sendpkm_actual()
	def _initserv_thread(self,foo,barr):
                self._initServ()
	def _sendpkm(self):
		if self.willhide.get()==True:
			self.withdraw()
		if self.serverrunning==False:
			thread.start_new_thread(self._sendpkm_thread,(1,1))
			self.serverrunning=True
	def _showWindow(self,event):
	    self.update()
	    self.deiconify()
	def _updateTableDisplay(self):
		self.tablevar.set(self.pokemontable.get_string()+"\n\nLast PKM:\n\n"+self.lastpkmtable.get_string())
	def __init__(self,initServ,sendpkm,getpkm,gtsvar):
		Tk.__init__(self)
		self._initServ = initServ
		self._sendpkm_actual = sendpkm
		self._getpkm_actual = getpkm
		self.gtsvar = gtsvar
		self.keeprunning = True
		self._hideme = self.withdraw
		self.willhide = IntVar()
		self.tablevar = StringVar()
		self.serverrunning=False
		self.pokemontable = PrettyTable([u"Pok\u00E9mon Species","Event"])
		self.lastpkmtable = PrettyTable([u"Pok\u00E9mon Species", "PID", "Sex", "Shiny", u"Pok\u00E9rus", "Nature"])
		self.frame = Frame(self)
		self.bind("<Control-Alt-Shift-Home>", self._showWindow)
		
		
		#-----Add Widgets Below this line-----
		self.sendbutton = Button(self.frame,text="Start Sending",command=self._sendpkm).grid(row=0, column=0)
		self.getbutton= Button(self.frame,text="Add From DS").grid(row=1, column=0)
		self.hidebox =  Checkbutton(self.frame, text="Hide window while hosting server (press Ctrl-C to show)",variable=self.willhide).grid(row=0, column=1,sticky=NW)
		self.table = Label(self.frame, textvariable=self.tablevar,font=("Courier New",8),justify=LEFT).grid(row=1, column=1,rowspan=5,sticky=SE)
		#-----End of Widgets at this line-----
		self.frame.grid(row=0,columnspan=2, sticky=NW+SE)
		self.title(u"PYE-GTS - Python Easy Global Trade Station - Pok\u00E9mon Options")
		self._updateTableDisplay()
                thread.start_new_thread(self._initserv_thread,(1,1))
